package com.xbongbong.paas.controller.workflow;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkFlowListDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowAddDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowArchiveDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowDeleteDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowDeleteVersionDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowEnableDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowGetDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowGetDetailDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowLinkListDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowNewVersionDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowSortDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowSortListDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowUpdateDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowUpdateNameDTO;
import com.xbongbong.paas.pojo.workflow.settings.dto.WorkflowVersionListDTO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkFlowListVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowAddVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowArchiveVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowDeleteVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowDeleteVersionVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowEnableVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowGetDetailVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowGetVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowLinkListVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowNewVersionVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowSortListVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowSortVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowUpdateNameVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowUpdateVO;
import com.xbongbong.paas.pojo.workflow.settings.vo.WorkflowVersionListVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.workflow.setting.WorkflowSetService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description: 工作流设置相关
 * @author 吴峰
 * @date 2021/5/10 14:44
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/workflowSet")
public class WorkflowSetController {

    @Resource
    private WorkflowSetService workflowSetService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     *
     * @param workFlowListDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/10 14:45
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid WorkFlowListDTO workFlowListDTO, BindingResult br) {
        XbbResponse<WorkFlowListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkFlowListVO workFlowListVO = workflowSetService.list(workFlowListDTO);
                response = new XbbResponse<>(workFlowListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * Description: 自定义工作流 关联对象列表
     * @param workflowLinkListDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2022/3/25 09:47
     * @since
     */
    @RequestMapping(value = "/linkList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String linkList(@RequestBody @Valid WorkflowLinkListDTO workflowLinkListDTO, BindingResult br) {
        XbbResponse<WorkflowLinkListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowLinkListVO wrkflowLinkListVO = workflowSetService.linkList(workflowLinkListDTO);
                response = new XbbResponse<>(wrkflowLinkListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     *
     * @param workflowAddDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/11 11:30
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid WorkflowAddDTO workflowAddDTO, BindingResult br) {
        XbbResponse<WorkflowAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(workflowAddDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(workflowAddDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                WorkflowAddVO workflowAddVO = workflowSetService.add(workflowAddDTO);
                response = new XbbResponse<>(workflowAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 获取工作流
     * @param workflowGetDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/19 16:38
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid WorkflowGetDTO workflowGetDTO, BindingResult br) {
        XbbResponse<WorkflowGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowGetVO workflowGetVO = workflowSetService.get(workflowGetDTO);
                response = new XbbResponse<>(workflowGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取工作流
     * @param workflowGetDetailDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/19 16:38
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/getDetail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getDetail(@RequestBody @Valid WorkflowGetDetailDTO workflowGetDetailDTO, BindingResult br) {
        XbbResponse<WorkflowGetDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowGetDetailVO workflowGetVO = workflowSetService.getDetail(workflowGetDetailDTO);
                response = new XbbResponse<>(workflowGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除工作流
     * @param workflowDeleteDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/14 10:43
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid WorkflowDeleteDTO workflowDeleteDTO, BindingResult br) {
        XbbResponse<WorkflowDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowDeleteVO workflowDeleteVO = workflowSetService.delete(workflowDeleteDTO);
                response = new XbbResponse<>(workflowDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     *
     * @param workflowEnableDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/14 16:22
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid WorkflowEnableDTO workflowEnableDTO, BindingResult br) {
        XbbResponse<WorkflowEnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowEnableVO workflowEnableVO = workflowSetService.enable(workflowEnableDTO);
                response = new XbbResponse<>(workflowEnableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     *
     * @param workflowUpdateNameDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/24 15:55
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/updateName", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateName(@RequestBody @Valid WorkflowUpdateNameDTO workflowUpdateNameDTO, BindingResult br) {
        XbbResponse<WorkflowUpdateNameVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowUpdateNameVO workflowUpdateNameVO = workflowSetService.updateName(workflowUpdateNameDTO);
                response = new XbbResponse<>(workflowUpdateNameVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     *
     * @param workflowSortListDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/25 14:04
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/sortList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sortList(@RequestBody @Valid WorkflowSortListDTO workflowSortListDTO, BindingResult br) {
        XbbResponse<WorkflowSortListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowSortListVO workflowSortListVO = workflowSetService.sortList(workflowSortListDTO);
                response = new XbbResponse<>(workflowSortListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     *
     * @param workflowSortDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/25 14:04
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sort(@RequestBody @Valid WorkflowSortDTO workflowSortDTO, BindingResult br) {
        XbbResponse<WorkflowSortVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowSortVO workflowSortVO = workflowSetService.sort(workflowSortDTO);
                response = new XbbResponse<>(workflowSortVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     *
     * @param workflowUpdateDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/25 14:04
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid WorkflowUpdateDTO workflowUpdateDTO, BindingResult br) {
        XbbResponse<WorkflowUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowUpdateVO workflowUpdateVO = workflowSetService.update(workflowUpdateDTO);
                response = new XbbResponse<>(workflowUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 版本列表
     * @param workflowVersionListDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/25 14:04
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/versionList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String versionList(@RequestBody @Valid WorkflowVersionListDTO workflowVersionListDTO, BindingResult br) {
        XbbResponse<WorkflowVersionListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowVersionListVO workflowUpdateVO = workflowSetService.versionList(workflowVersionListDTO);
                response = new XbbResponse<>(workflowUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 归档工作流
     * @param workflowArchiveDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/25 14:04
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/archive", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String archive(@RequestBody @Valid WorkflowArchiveDTO workflowArchiveDTO, BindingResult br) {
        XbbResponse<WorkflowArchiveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowArchiveVO workflowArchiveVO = workflowSetService.archive(workflowArchiveDTO);
                response = new XbbResponse<>(workflowArchiveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 添加新版本
     * @param workflowNewVersionDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/5/25 14:04
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/newVersion", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String newVersion(@RequestBody @Valid WorkflowNewVersionDTO workflowNewVersionDTO, BindingResult br) {
        XbbResponse<WorkflowNewVersionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowNewVersionVO workflowNewVersionVO = workflowSetService.newVersion(workflowNewVersionDTO);
                response = new XbbResponse<>(workflowNewVersionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除新版本
     * @param workflowDeleteVersionDTO
     * @param br
     * @return
     * @throws
     * @author 吴峰
     * @date 2021/6/1 17:18
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/deleteVersion", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteVersion(@RequestBody @Valid WorkflowDeleteVersionDTO workflowDeleteVersionDTO, BindingResult br) {
        XbbResponse<WorkflowDeleteVersionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkflowDeleteVersionVO workflowNewVersionVO = workflowSetService.deleteVersion(workflowDeleteVersionDTO);
                response = new XbbResponse<>(workflowNewVersionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
